package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.MultiItemTypeAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.ReserveOrderEntity;
import cn.mumzone.pos.model.ServiceEntity;

/**
 * Created by JoeLjt on 2019/6/13.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class ADA_CashServiceList extends CommonAdapter<ReserveOrderEntity> {


    public ADA_CashServiceList(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_cash_list;
    }

    @Override
    protected void convert(ViewHolder holder, ReserveOrderEntity reserveOrderEntity, int position) {

        holder.setText(R.id.tv_name, !StringUtils.isEmpty(StringUtils.get(reserveOrderEntity.getUserName()))
                ? StringUtils.getUserName(reserveOrderEntity.getUserName()) :
                !reserveOrderEntity.isUserIsMember() ? "散客" : "暂无姓名");
        holder.setText(R.id.tv_phone, StringUtils.get(reserveOrderEntity.getUserPhone()));
        holder.setText(R.id.tv_appointment_time,
                AppDateUtil.getFriendlyDateTime(reserveOrderEntity.getAppointmentTime(), AppDateUtil.HH_MM, AppDateUtil.MM_DD_HH_MM2));
        LinearLayout llServices = holder.getView(R.id.ll_services);
        LinearLayout llServiceStatus = holder.getView(R.id.ll_service_status);
        switch (reserveOrderEntity.getStatus()) {
            case ReserveOrderEntity.ORDER_STATUS_ARRIVED:
            case ReserveOrderEntity.ORDER_STATUS_PROCESSING:
                llServiceStatus.setBackgroundResource(R.drawable.shape_bg_item_cash_status_service);
                break;
            case ReserveOrderEntity.ORDER_STATUS_RESERVED:
                llServiceStatus.setBackgroundResource(R.drawable.shape_bg_item_cash_status_reserve);
                break;
            default:
                llServiceStatus.setBackgroundResource(R.drawable.shape_bg_item_cash_status_close);
                break;
        }

        llServices.removeAllViews();
        double totalPrice = 0;
        if (!ListUtil.isListEmpty(reserveOrderEntity.getServices())) {
            for (int index = 0; index < reserveOrderEntity.getServices().size(); index++) {
                ServiceEntity serviceEntity = reserveOrderEntity.getServices().get(index);
                totalPrice = BigDecimalUtil.add(String.valueOf(totalPrice),
                        !StringUtils.isEmpty(reserveOrderEntity.getBillingUser())
                                && MemberEntity.DISCOUNT_TYPE_MEMBER_PRICE.equals(StringUtils.get(reserveOrderEntity.getBillDiscountType()))
                                ? serviceEntity.getMemberPrice() : serviceEntity.getOriginPrice());
                if (index > 3) {
                    continue;
                }
                if (index < 3) {
                    View view = LayoutInflater.from(mContext).inflate(R.layout.item_cash_service_list, llServices, false);
                    TextView tvServiceName = view.findViewById(R.id.tv_service_name);
                    TextView tvServicePrice = view.findViewById(R.id.tv_service_price);
                    tvServiceName.setText(StringUtils.get(serviceEntity.getServiceName()));
                    tvServicePrice.setText(StringUtils.getFormattedMoneyWithUnit(
                            !StringUtils.isEmpty(reserveOrderEntity.getBillingUser())
                                    && MemberEntity.DISCOUNT_TYPE_MEMBER_PRICE.equals(StringUtils.get(reserveOrderEntity.getBillDiscountType()))
                                    ? serviceEntity.getMemberPrice() : serviceEntity.getOriginPrice()));
                    llServices.addView(view);
                } else {
                    View view = LayoutInflater.from(mContext).inflate(R.layout.item_cash_service_list_more, llServices, false);
                    TextView tvCount = view.findViewById(R.id.tv_count);
                    tvCount.setText(String.format("共%s项", reserveOrderEntity.getServices().size()));
                    llServices.addView(view);
                }
            }
        }
        holder.setText(R.id.tv_total_price, StringUtils.getFormattedMoneyWithUnit(String.valueOf(totalPrice)));
    }

}
